package cn.crm.system.service.impl;

import cn.crm.common.utils.SecurityUtils;
import cn.crm.system.domain.pojo.dto.CaseStatesDto;
import cn.crm.system.domain.pojo.dto.QuotedStatesDto;
import cn.crm.system.domain.pojo.entity.CaseStates;
import cn.crm.system.domain.pojo.entity.QuotedStates;
import cn.crm.system.domain.pojo.vo.CaseStatesVo;
import cn.crm.system.domain.pojo.vo.QuotedStatesVo;
import cn.crm.system.mapper.CaseStatesMapper;
import cn.crm.system.mapper.QuotedStatesMapper;
import cn.crm.system.service.ICaseStatesService;
import cn.crm.system.service.IQuotedStatesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 处理项目状态关联的接口
 */

@Slf4j
@Service
public class CaseStatesServiceImpl implements ICaseStatesService {

    @Autowired
    private CaseStatesMapper CaseStatesMapper;


    @Override
    public int insertCaseStates(CaseStatesDto caseStatesDto) {
        // 通过quotedId查询是否存在记录
        CaseStatesVo existingState = CaseStatesMapper.selectBycaseId(caseStatesDto.getCaseId());

        // 设置操作人
        caseStatesDto.setCreateBy(SecurityUtils.getUsername());


        CaseStates caseStates = new CaseStates();
        BeanUtils.copyProperties(caseStatesDto, caseStates);

        int result;
        if (existingState != null) {
            // 存在记录，执行更新操作
            log.debug("更新报价单状态，quotedId: {}", caseStatesDto.getCaseId());
            caseStates.setId(existingState.getId());
            caseStates.setUpdateBy(SecurityUtils.getUsername());
            caseStates.setUpdateTime(new Date());
            result = CaseStatesMapper.updateCaseStates(caseStates);
        } else {
            // 不存在记录，执行新增操作
            log.debug("新增报价单状态，quotedId: {}", caseStatesDto.getCaseId());
            caseStates.setCreateTime(new Date());
            result = CaseStatesMapper.insertCaseStates(caseStates);
        }

        return result;
    }
    }


